package com.prototype.vaadin;

import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 *
 * @author namounis
 */
public class CmdLineRecover {
    
    public CmdLineRecover(){
        
    }
   
    /* Step 1 : recupére le lien de la page qui contient la ligne de commande */  
    public void getCodeURL(String urlPage) throws IOException{
        CmdLineRecover c = new CmdLineRecover();
        
        Document sf_page = Jsoup.connect(urlPage).timeout(0).get();
        
        int i = 0;
        int size = sf_page.select("li a").size();
        
        while(i < size){
            Element result = sf_page.select("li a").get(i);
            
              if(result.text().toString().contains("Code")){
                  c.getCmdLine(result.absUrl("href"));
              }
              
              if(result.text().toString().equals("Svn")){
                  c.getCmdLine(result.absUrl("href"));
              }
              
              if(result.text().toString().equals("Cvs")){ 
                  c.getCmdLine(result.absUrl("href"));
              } 
              
              if(result.text().toString().equals("Git")){
                  c.getCmdLine(result.absUrl("href"));
              }
              i++;
        } 
    }
    
     /* Step 2 : recupére la ligne de commande selon la structure de la page */  
    public void getCmdLine(String urlPage) throws IOException{
        
        Document sf_page = Jsoup.connect(urlPage).timeout(0).get();
        
        /* case 1 */
        Elements result = sf_page.select("div .grid-16 code");
        
        if(!result.text().equals("")){
            System.out.println(result.text());
        }

        /* case 2 */       
        Elements result1 = sf_page.select("div .yui-u code");
        
        if(!result1.text().equals("")){
            System.out.println(result1.text());
        } 
        
        /* case 3 */
        Elements result2 = sf_page.select("div .btn-set a");
        
        if(!result2.text().equals("")){
            System.out.println(result2.attr("data-url"));  
        }

    }
    
    
}
